home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / gxcht.c < prev    next >
C/C++ Source or Header  |  1993-05-13  |  6KB  |  209 lines

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gxcht.c */
  20. /* Color halftone setup for Ghostscript imaging library */
  21. /****** NOT USED YET, DON'T TAKE TOO SERIOUSLY ******/
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gxfixed.h"
  25. #include "gxmatrix.h"            /* for gxdevice.h */
  26. #include "gzstate.h"
  27. #include "gzdevice.h"
  28. #include "gzcolor.h"            /* requires gxdevice.h */
  29. #include "gzht.h"
  30.  
  31. #include "gxdevmem.h"
  32.  
  33. /*
  34.  * We construct color halftone tiles out of 3 or 4 "planes".
  35.  * Each plane specifies halftoning for one component (R/G/B or C/M/Y/K).
  36.  */
  37.  
  38. void
  39. set_color_ht(
  40.     gx_bitmap *ctile,    /* the output tile; data, raster, size are set */
  41.     int px,        /* the initial phase of the output tile */
  42.     int py,
  43.     int w,        /* how much of the tile to set */
  44.     int h,
  45.     gx_device *dev,    /* for color mapping */
  46.     gx_device_color planes[4],    /* actually nplanes */
  47.     int nplanes    /* 3 or 4 */
  48. )
  49. {    /* Note that the planes are specified in the order RGB or CMYK, but */
  50.     /* the indices used for the internal colors array are BGR or KYMC. */
  51.  
  52.     gx_color_index colors[16];    /* the actual colors for the tile */
  53.  
  54.     /* Set up the tile colors. */
  55.     {    gx_color_value r0 = planes[0].color1;
  56.         gx_color_value r1 = planes[0].color2;
  57.         gx_color_value g0 = planes[1].color1;
  58.         gx_color_value g1 = planes[1].color2;
  59.         gx_color_value b0 = planes[2].color1;
  60.         gx_color_value b1 = planes[2].color2;
  61. #define map8(m)\
  62.   m(0, r0, g0, b0); m(1, r1, g0, b0);\
  63.   m(2, r0, g1, b0); m(3, r1, g1, b0);\
  64.   m(4, r0, g0, b1); m(5, r1, g0, b1);\
  65.   m(6, r0, g1, b1); m(7, r1, g1, b1)
  66.         if ( nplanes == 3 )
  67.         {    dev_proc_map_rgb_color((*map)) =
  68.                 dev->procs->map_rgb_color;
  69. #define mapc(i, r, g, b)\
  70.   colors[i] = (*map)(dev, r, g, b)
  71.             map8(mapc);
  72. #undef mapc
  73.         }
  74.         else
  75.         {    dev_proc_map_cmyk_color((*map)) =
  76.                 dev->procs->map_cmyk_color;
  77.             gx_color_value k0 = planes[3].color1;
  78.             gx_color_value k1 = planes[3].color2;
  79. #define mapc(i, r, g, b)\
  80.   colors[i] = (*map)(dev, r, g, b, k0);\
  81.   colors[i+8] = (*map)(dev, r, g, b, k1)
  82.             map8(mapc);
  83. #undef mapc
  84.         }
  85. #undef map8
  86.     }
  87.  
  88.     /* Construct the actual tile. */
  89.     {    int x, y;
  90.         struct tile_cursor_s {
  91.             int xoffset;
  92.             int xshift;
  93.             const byte *row;
  94.             const byte *tdata;
  95.             uint raster;
  96.             const byte *data;
  97.             uint shifter;
  98.         } cursor[4];
  99.         /* Note that depth must be at least 4 */
  100.         /* (4, 8, 16, 24, 32). */
  101.         int depth = dev->color_info.depth;
  102.         int dbytes = depth >> 3;
  103.         uint dest_raster = ctile->raster;
  104.         byte *dest_row =
  105.           ctile->data + dest_raster * (h - 1) + (w * depth - 1) / 8;
  106.         {    int lastx = w - 1 + px;
  107.             int lasty = h - 1 + py;
  108. #define set_start(c, i)\
  109. { gx_bitmap *btile = planes[i].tile;\
  110.   int bx = lastx % btile->size.x;\
  111.   int by = lasty % btile->size.y;\
  112.   c.xoffset = bx >> 3;\
  113.   c.xshift = ~bx & 7;\
  114.   c.tdata = btile->data;\
  115.   c.raster = btile->raster;\
  116.   c.row = c.tdata + by * c.raster;\
  117. }
  118.             set_start(cursor[0], 0);
  119.             set_start(cursor[1], 1);
  120.             set_start(cursor[2], 2);
  121.             if ( nplanes == 4 )
  122.                 set_start(cursor[3], 3);
  123. #undef set_start
  124.         }
  125.         for ( y = h; --y >= 0; dest_row -= dest_raster )
  126.         {    byte *dest = dest_row;
  127. #define set_row(c, i)\
  128.   {    c.data = c.row + c.xoffset;\
  129.     c.shifter = ((*c.data + 0x100) >> c.xshift) << i;\
  130.   }
  131.             set_row(cursor[0], 0);
  132.             set_row(cursor[1], 1);
  133.             set_row(cursor[2], 2);
  134.             if ( nplanes == 4 )
  135.             {    set_row(cursor[3], 3);
  136.             }
  137.             else
  138.                 cursor[3].shifter = 0;
  139. #undef set_row
  140.             for ( x = w; --x >= 0; )
  141.             {    int c =
  142.                     (cursor[0].shifter & 1) +
  143.                     (cursor[1].shifter & 2) +
  144.                     (cursor[2].shifter & 4);
  145.                 gx_color_index tcolor;
  146. #define step_plane(c, i)\
  147.   if ( (c.shifter >>= 1) < (2 << i) )\
  148.   {    if ( c.data > c.row )\
  149.         c.shifter = (*--c.data + 0x100) << i;\
  150.     else\
  151.     {    int tw1 = planes[i].tile->size.x - 1;\
  152.         c.data = c.row + (tw1 >> 3);\
  153.         c.shifter = ((*c.data + 0x100) >> (~tw1 & 7)) << i;\
  154.     }\
  155.   }
  156.                 step_plane(cursor[0], 0);
  157.                 step_plane(cursor[1], 1);
  158.                 step_plane(cursor[2], 2);
  159.                 if ( nplanes == 4 )
  160.                 {    c += cursor[3].shifter & 8;
  161.                     step_plane(cursor[3], 3);
  162.                 }
  163. #undef step_plane
  164.                 tcolor = colors[c];
  165.                 switch ( dbytes )
  166.                 {
  167.                 case 0:            /* 4 */
  168.                     if ( x & 1 )
  169.                         *dest = (byte)tcolor;
  170.                     else
  171.                         *dest-- += (byte)tcolor << 4;
  172.                     break;
  173.                 case 4:            /* 32 */
  174.                     dest -= 4;
  175.                     dest[3] = (byte)tcolor;
  176.                     dest[2] = (byte)((uint)tcolor >> 8);
  177.                     tcolor >>= 16;
  178.                     goto c2;
  179.                 case 3:            /* 24 */
  180.                     dest -= 3;
  181.                     dest[2] = (byte)tcolor;
  182.                     dest[1] = (byte)((uint)tcolor >> 8);
  183.                     tcolor >>= 16;
  184.                     goto c2;
  185.                 case 2:            /* 16 */
  186.                     dest -= 2;
  187. c2:                    dest[1] = (byte)tcolor;
  188. c1:                    dest[0] = (byte)((uint)tcolor >> 8);
  189.                     break;
  190.                 case 1:            /* 8 */
  191.                     *dest-- = (byte)tcolor;
  192.                 }
  193.             }
  194. #define step_row(c, i)\
  195.   if ( c.row > c.tdata )\
  196.     c.row -= c.raster;\
  197.   else\
  198.     c.row += c.raster * (planes[i].tile->size.y - 1)
  199.             step_row(cursor[0], 0);
  200.             step_row(cursor[1], 1);
  201.             step_row(cursor[2], 2);
  202.             if ( nplanes == 4)
  203.                 step_row(cursor[3], 3);
  204. #undef step_row
  205.         }
  206.     }
  207.  
  208. }
  209.